package br.com.lopes.gci.log;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

import br.com.lopes.gci.exception.DAOException;

@Aspect
public class ExceptionLogging {
	private static final Logger LOGGER = Logger.getLogger(ExceptionLogging.class.getName());

	@Pointcut("execution(* br.com.souzacruz.risksystem.route.service.*.*(..)) && !within(ExceptionLogging)")
	public void logExceptions() { }

    @AfterThrowing(pointcut = "logExceptions()", throwing = "t") 
    public void logException(JoinPoint jp, Throwable t) {
    	Signature signature = jp.getSignature();
    	if (t instanceof DAOException) {
	    	LOGGER.error(String.format(
					"Data Base exception at class [%s], method: [%s]. Exception was [%s]", new Object[] {
							signature.getDeclaringTypeName(), signature.getName(), t }), t);
    	} else {
	    	LOGGER.error(String.format(
					"Unexpected error at class [%s], method: [%s]. Exception was [%s]", new Object[] {
							signature.getDeclaringTypeName(), signature.getName(), t }), t);
    	}
    }
}